package com.hrt.datasender.datastorage.strategy.status;

import com.btcode.common.MyLog;
import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.result.Row;
import com.btcode.log.ILog;
import com.btcode.orm.model.IModel;
import com.btcode.schedule.ScheduleManager;
import com.btcode.schedule.SingleInstanceJob;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.model.BdDeviceStateModel;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.datastorage.strategy.IStorageStrategy;
import com.hrt.db.DBManager;
import org.quartz.JobExecutionContext;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DeviceStatusStrategy extends SingleInstanceJob implements IStorageStrategy {
    private ILog log;

    @Override
    public void init() {
        log= MyLog.getInstance().getLogger(getClass());
        ScheduleManager.getInstance().addJob(getClass(), "0 0/1 * * * ?");

    }



    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {
        return null;
    }

    /**
     * 获取准备好要发送的数据时间
     * 
     * @return
     */
    protected Long getReadySendDataTime(BcSendPlatformModel platform) {
        Date startDate = getLastSendTime(platform);

        String sql = "SELECT * FROM BD_MO_DEVICE_STATE WHERE DATATIME > " + startDate.getTime()
                + " ORDER BY DATATIME ASC LIMIT 0,1";

        List<Row> list = DBManager.getDataExecuter().queryList(sql, null);

        if (list.size() < 1) {
            return null;
        }

        long firstDate = (long) list.get(0).get("DATATIME");

        return firstDate;
    }

    @Override
    public List<Row> getSendData(BcSendPlatformModel sendPlatForm) {
        Long readySendDatatime = getReadySendDataTime(sendPlatForm);

        if (readySendDatatime == null) {
            return null;
        }

        String sql = " SELECT * FROM BD_MO_DEVICE_STATE WHERE DATATIME = " + readySendDatatime;

        List<Row> result = DBManager.getDataExecuter().queryList(sql, null);

        Date date = new Date(readySendDatatime);

        BdSendRecordService.getInstance().updateLastSendDate(StorageDataType.STATUS, sendPlatForm.getIp(),
                sendPlatForm.getPort(), date);

        return result;
    }

    /**
     * 获取最后读取的时间
     * 
     * @return
     */
    public Date getLastSendTime(BcSendPlatformModel platform) {
        Date result = BdSendRecordService.getInstance().getLastSendDate(getDataType(), platform.getIp(),
                platform.getPort());
        return result;
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.STATUS;
    }

    @Override
    public String getNote() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getShowName() {
        // TODO Auto-generated method stub
        return "设备状态";
    }

    @Override
    public int getDataInterval() {
        return 60;
    }

    @Override
    public String getStoageTable() {
        return "BD_MO_DEVICE_STATE";
    }

    @Override
    public void initJob(JobExecutionContext arg0) {

    }

    @Override
    public void myExecute(JobExecutionContext arg0) {

    }
}
